
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 10

void main12() {
    int i, j, n, s, a[N][N] = {0};
    printf("请输入 n: ");
    scanf("%d", &n);

    for (i = 0; i < n; i++)          /*生成n*n的矩阵*/
    {
        for (j = 0; j < n; j++)
            a[i][j] = i * n + j + 1;
        printf("%d", a[i][j]);
    }
    getchar();
    s = 0;
    for (i = 0; i < n; i++)          /*求和对角线元素a[i][i]*/
        s += a[i][i];

    printf("对角线元素和为%d. ", s);
}

void main13() {
    int a[3][4] = {1, 3, 5, 7, 9, 11, 13, 15,
                   17, 19, 21, 23};
    int (*p)[4], i, j;
    p = a;
    printf("enter row and colum:");
    scanf("%d,%d", &i, &j);
    printf("a[%d],[%d]=%d\n",
           i, j, *(*(p + i) + j));
}


void main14() {
    int i;
    for (i = 1; i < 100; i++);
    {
        char o;
        float num1, num2;
        printf("Enter operator either + or - or * or / : ");
        scanf("%c", &o);
        printf("Enter two operands: ");
        scanf("%f%f", &num1, &num2);
        switch (o) {
            case '+':
                printf("%.1f + %.1f = %.1f", num1, num2, num1 + num2);
                break;
            case '-':
                printf("%.1f - %.1f = %.1f", num1, num2, num1 - num2);
                break;
            case '*':
                printf("%.1f * %.1f = %.1f", num1, num2, num1 * num2);
                break;
            case '/':
                printf("%.1f / %.1f = %.1f", num1, num2, num1 / num2);
                break;
            default:
                /* If operator is other than +, -, * or /, error message is shown */
                printf("Error! operator is not correct");
                break;
        }
    }
}


void main15() {
/*	char a[100]="273828627328327826383223828223832448324437225";
	char b[100]="3148253272283273832434328323243832823424328343286582";*/
    char a[100] = "5678";
    char b[100] = "5678";


    char c[100] = {0};
    int m = strlen(a) - 1, n = strlen(b) - 1;
    int i, j;
    int x, y = 0;
    j = (m < n ? m : n);
    for (i = 0; i <= j; i++, m--, n--);
    {
        x = a[m] + b[n];
        c[i] = x % 10 + y;
        y = x / 10;
    }

    if (m > n) {
        c[i] = a[m] + y;
        for (i++; i < strlen(a); i++, m--)
            c[i] = a[m];
    } else {
        c[i] = b[n] + y;
        for (i++; i < strlen(b); i++, n--)
            c[i] = a[n];
    }
    int k = strlen(c);
    for (; k >= 0; k--)
        printf("%1d", c[k]);

}

void main16() {
    void move(int *p);

//	int a[3][3]={1,2,3,4,5,6,7,8,9};
    int a[3][3];
    int *p;
    int i, j, t;
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            scanf("%d", &a[i][j]);
    p = &a[0][0];
    move(p);
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            printf("%3d", *(p + i * 3 + j));
        printf("\n");
    }
}


void move(int *p) {
    int i, j, t;
    for (i = 0; i < 3; i++)
        for (j = i; j < 3; j++) {
            t = *(p + 3 * i + j);
            *(p + 3 * i + j) = *(p + 3 * j + i);
            *(p + 3 * j + i) = t;
            /*
             * t=*(*(p+i)+j);
             * (*(p+i)+j)=(*(p+j)+i);
             * (*(p+j)+i)=t;
             */

        }

}

void main17() {
    int i, j;
    printf("\n     ");
    for (i = 1; i <= 9; i++)
        printf("%4d", i);
    printf("\n----------------------------------------");
    for (i = 1; i <= 9; i++) {
        printf("\n%4d|", i);
        for (j = 1; j <= i; j++)
            printf("%4d", i * j);
    }
}



/*
奇魔方（阶数n = 2 * m + 1,m=1，2，3……）
规律如下：

数字1位于方阵中的第一行中间一列；
数字a（1 < a  ≤ n2）所在行数比a-1行数少1，若a-1的行数为1，则a的行数为n；

数字a（1 < a  ≤ n2）所在列数比a-1列数大1，若a-1的列数为n，则a的列数为1；

如果a-1是n的倍数，则a（1 < a  ≤ n2）的行数比a-1行数大1，列数与a-1相同。


*/
#include<stdio.h>

void main18() {
    int a[20][20], i, j, k, p = 1, n;
    while (p == 1) {
        printf("Enter n(n=1--19):");
        scanf("%d", &n);
        if (n > 0 && n <= 19 && n % 2 != 0)
            p = 0;
    }
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            a[i][j] = 0;
    j = n / 2 + 1;
    a[1][j] = 1;
    for (k = 2; k <= n * n; k++) {
        i -= 1;
        j += 1;
        if (i < 1 && j > n) {
            i += 2;
            j -= 1;
        } else {
            if (i < 1)i = n;
            if (j > n)j = 1;
        }
        if (a[i][j] == 0)
            a[i][j] = k;
        else {
            i += 2;
            j -= 1;
            a[i][j] = k;
        }
    }
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++)
            printf("%4d", a[i][j]);
        printf("\n\n");
    }
}

void main19() {
    int a[20][20] = {0};
    int i, j, k, p = 1, n;
    while (p == 1) {
        printf("Enter n(1--19):");
        scanf("%d", &n);
        if (n > 0 && n <= 19 && n % 2 != 0)
            p = 0;
    }
    j = n / 2;
    i = 0;
    a[i][j] = 1;
    for (k = 2; k <= n * n; k++) {
        if ((k - 1) % n != 0) {
            if (i != 0) i -= 1;
            else i = n - 1;
            if (j != n - 1) j += 1;
            else j = 0;
        } else {
            if (i != n - 1) i += 1;
            else i = 0;
        }
        a[i][j] = k;
    }
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++)
            printf("%4d", a[i][j]);
        printf("\n\n");
    }
}

void main11() {


}